package com.dongdongshop.controller;


import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;

import com.dongdongshop.pojo.TbUser;

import com.dongdongshop.service.UserService;
import com.dongdongshop.utils.ShiroUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/login")
@Controller
public class LoginController {

    @DubboReference
    private UserService tbUserService;

    @RequestMapping("/toLogin")
    public String tologin(){
        return "login";
    }

    @RequestMapping("loginUser")
    @ResponseBody
    public Result login(String username, String password){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        //执行登录方法
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            return Result.response(ResultEnum.NOT_FOUND_ACCOUNT);
        }catch (IncorrectCredentialsException e){
            return Result.response(ResultEnum.ER_PASSWORD);
        }
        return Result.OK();
    }

}
